package com.page.model;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.page.entity.Car;

/**
 * 
* @author dinghao
* @ClassName: CarCriteria 
* @Description: 分页的条件
* @date 2018年3月21日 下午3:17:52 
*
 */
public class CarCriteria implements Specification<Car>{

	private String vechileBrand;

	private String vechileColor;
	
	private String vechileNo;
	
	private String vechileType;

	public String getVechileBrand() {
		return vechileBrand;
	}

	public void setVechileBrand(String vechileBrand) {
		this.vechileBrand = vechileBrand;
	}

	public String getVechileColor() {
		return vechileColor;
	}

	public void setVechileColor(String vechileColor) {
		this.vechileColor = vechileColor;
	}

	public String getVechileNo() {
		return vechileNo;
	}

	public void setVechileNo(String vechileNo) {
		this.vechileNo = vechileNo;
	}

	public String getVechileType() {
		return vechileType;
	}

	public void setVechileType(String vechileType) {
		this.vechileType = vechileType;
	}

	@Override
	public Predicate toPredicate(Root<Car> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		Predicate predicate = cb.conjunction();
		if (StringUtils.isNotEmpty(vechileBrand)) {
			predicate=cb.and(predicate,cb.like(root.get("vechileBrand").as(String.class),"%" + vechileBrand + "%"));
		}
		if (StringUtils.isNotEmpty(vechileType)) {
			predicate=cb.and(predicate, cb.like(root.get("vechileType").as(String.class),"%"+ vechileType +"%"));
		}
		if (StringUtils.isNotEmpty(vechileNo)) {
			predicate=cb.and(predicate, cb.like(root.get("vechileNo").as(String.class), "%"+ vechileNo +"%"));
		}
		if (StringUtils.isNotEmpty(vechileColor)) {
			predicate=cb.and(predicate, cb.equal(root.get("vechileColor").as(String.class),vechileColor));
		}
		return predicate;
	}
	
	
}
